package main

import (
    "fmt"
    "net"
    "time"
)

const (
    IEC104_PORT = 2404
)

type IEC104Message struct {
    Type    string
    Data    string
    AuthKey string
}

func sendIEC104Message(conn net.Conn, message IEC104Message) error {
    data := fmt.Sprintf("%s|%s|%s", message.Type, message.Data, message.AuthKey)
    _, err := conn.Write([]byte(data))
    return err
}

func receiveIEC104Message(conn net.Conn) (IEC104Message, error) {
    buffer := make([]byte, 1024)
    n, err := conn.Read(buffer)
    if err != nil {
        return IEC104Message{}, err
    }
    parts := string(buffer[:n])
    data := parts[:len(parts)-1]
    return IEC104Message{
        Type:    data[0:1],
        Data:    data[2:4],
        AuthKey: data[5:],
    }, nil
}